package com.baizhi.controller;

import com.baizhi.dto.AlbumDto;
import com.baizhi.dto.AudioDto;
import com.baizhi.entity.Album;
import com.baizhi.entity.Audio;
import com.baizhi.service.AlbumDtoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2019/4/9.
 */
@Controller
@RequestMapping("AlbumDtoController")
public class AlbumDtoController {
    @Autowired
    private AlbumDtoService albumDtoService;

    @RequestMapping("selectAllAlbumDao.do")
    @ResponseBody
    public List<AlbumDto> selectAllAlbumDao(){
        return albumDtoService.selectAllAlbumDao();
    }


    @RequestMapping("addAudio.do")
    @ResponseBody
    public Map addAudio(HttpServletRequest request, Audio audio, MultipartFile addAudioImage){
        System.out.println(audio+"---------------");
        if(addAudioImage!=null){
            String fileName=addAudioImage.getOriginalFilename();
            String st = UUID.randomUUID().toString();
            String newName=st+fileName;
            String realPathNewMethod = request.getServletContext().getRealPath("audio");
            try {
                addAudioImage.transferTo(new File(realPathNewMethod+"/"+newName));
            } catch (IllegalStateException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            audio.setAudioUrl("/audioCollection/"+newName);
            return  albumDtoService.addAudio(audio);
        }else {
            audio.setAudioUrl("/img/audioCollection"+"1.jpg");
            return  albumDtoService.addAudio(audio);
        }
    }
}
